import { Body, Controller, Post, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
// import { SysMenuService } from '../menu/menu.service';
import { SysUserService } from './user.service';
import SysUser from '@main/entities/service/admin/sys/sys-user.entity';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('管理员模块')
@Controller('user')
export class SysUserController {
  constructor(
    private userService: SysUserService // private menuService: SysMenuService,
  ) {}

  @ApiOperation({ summary: '获取用户列表' })
  @ApiOkResponse({ type: [SysUser] })
  @Post('list')
  async list() {
    return await this.userService.list();
  }

  @ApiOperation({ summary: '分页获取获取用户列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto, @AdminUser() user) {
    const [list, total] = await this.userService.page(user.uid, dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加用户' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.userService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新用户' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.userService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除用户' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', syncHostSqlite = '' } = req.headers;
    await this.userService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '用户更新密码' })
  @ApiOkResponse()
  @Post('updatePassword')
  async password(@Body() dto, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite:syncHostSqlite = '' } = req.headers;
    await this.userService.forceUpdatePassword(dto, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '获取用户信息' })
  @ApiOkResponse()
  @Post('info')
  async info(@Body() dto) {
    return await this.userService.info(dto);
  }
}
